#!/usr/bin/perl

use strict;
use warnings;
use File::Basename;

my $MLPROD_HOME = $ENV{'MLPROD_HOME'};
my $PGNAME=basename($0);
my $PGDIRNAME=dirname($0);
my $PGERR=$PGNAME."[$$]";
my $PGPWD =$ENV{'PWD'};
chdir $PGDIRNAME;
my $PGABSDIRNAME=$ENV{'PWD'};
chdir $PGPWD;



sub ssn_ok($)
{
  my $ssn = shift;

  if( $ssn !~ /^\d{9}$/) { return 0; }

  foreach ('000000000', '111111111', '222222222',
           '333333333', '444444444', '555555555',
           '666666666', '777777777', '888888888',
           '999999999', '123456789', '078051120')
  {
     if ( $ssn =~ /$_/ ) { return 0; }
  }


 if( substr($ssn,0,3) eq '000'  ||
     substr($ssn,3,2) eq '00'   ||
     substr($ssn,5,4) eq '0000' ||
     substr($ssn,0,3) eq '666'  ||
     substr($ssn,0,3) > 800 )
 {
     return 0;
 }

 return 1;

}



my $linenum = 0;


while(<STDIN>) {
  my $line = $_;
  my $cust_id = "";
  my $hid = "";
  my $acctnum = "";
  my $name_type = "";
  my $efx_ssn = "";
  my $delim = "|";

  $linenum++;

  chomp($line);

  $cust_id   = substr($line,178,4);
  $hid       = substr($line,170,6);
  $acctnum   = substr($line,150,20);
  $name_type = substr($line,176,1);
  $efx_ssn   = substr($line,224,9);

  $cust_id =~ s/ //g;
  $hid =~ s/ //g;
  $acctnum =~ s/ //g;
  $efx_ssn =~ s/ //g;

  if (! ::ssn_ok($efx_ssn) ) { $efx_ssn = "";}

  print $cust_id, $delim, $hid, $delim, $acctnum, $delim, $name_type, $delim, $efx_ssn;
  print "\n";
}
